package com.erp.erp_servers.bill_declare.imp;

import com.erp.erp_dao.bill_declareDao.IBillDeclareInfoDao;
import com.erp.erp_entitys.Drawback.entity.DrawbackEntity;
import com.erp.erp_entitys.bill_declare.*;
import com.erp.erp_entitys.business.entity.CarryDetailEntity;
import com.erp.erp_entitys.business.entity.CarryDetailProductEntity;
import com.erp.erp_entitys.business.entity.CarryDetailProductPartsEntity;
import com.erp.erp_servers.Drawback.DrawbackService;
import com.erp.erp_servers.bill_declare.BillDeclareInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * 报关主信息表(BillDeclareInfo)表服务实现类
 *
 * @author makejava
 * @since 2021-01-08 17:41:53
 */
@Service("billDeclareInfoService")
public class BillDeclareInfoServiceImpl implements BillDeclareInfoService {
    @Resource
    private IBillDeclareInfoDao billDeclareInfoDao;
    @Resource
    private DrawbackService drawbackService;

    /**
     * 通过ID查询单条数据
     *
     * @param declareId 主键
     * @return 实例对象
     */
    @Override
    public BillDeclareInfo queryInfoById(Integer declareId) {
        return this.billDeclareInfoDao.queryInfoById(declareId);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<BillDeclareInfo> queryAllInfoByLimit(int offset, int limit) {
        return this.billDeclareInfoDao.queryAllInfoByLimit(offset, limit);
    }

    @Override
    public List<BillDeclareInfo> selectAllInfo() {
        return billDeclareInfoDao.selectAllInfo();
    }

    @Override
    public List<BillDeclareInfo> selectAllInfobySearch(InfoSearch search) {
        return  billDeclareInfoDao.selectAllInfobySearch(search);
    }

    /**
     * 新增数据
     *
     * @param billDeclareInfo 实例对象
     * @return 实例对象
     */
    @Override
    public BillDeclareInfo insertInfo(BillDeclareInfo billDeclareInfo) {
        this.billDeclareInfoDao.insertInfo(billDeclareInfo);
        return billDeclareInfo;
    }

    /**
     * 修改数据
     *
     * @param billDeclareInfo 实例对象
     * @return 实例对象
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public BillDeclareInfo updateInfo(BillDeclareInfo billDeclareInfo) {
        this.billDeclareInfoDao.updateInfo(billDeclareInfo);
        BillDeclareInfo billDeclareInfo1=this.queryInfoById(billDeclareInfo.getDeclareId());

        Integer billId=billDeclareInfo1.getDeclareId();
        //根据报表ID查询退税表，返回实体
        DrawbackEntity drawback=drawbackService.selectByDeclareId(billId);
        switch (billDeclareInfo1.getStatus()){
            //1，2，5状态都是删除
            case 1:
            case 2:
            case 5:
                drawbackService.deleteDrawbackByBillDeclareInfoId(billId);
                break;
            case 3:
            case 4:
                //完成箱退回到已批箱：修改
                //已批箱提升到完成箱：修改
                //已批箱退回到待批箱：删除
                //待批箱提升到已批箱：添加
                if (drawback!=null){
                    drawbackService.updateDrawbackByBillDeclareInfoId(billId);
                }else{
                    drawbackService.insertBillDeclareInfoToDrawback(billId);
                }
                break;
        }
        return  billDeclareInfo1;
    }

    /**
     * 通过主键删除数据
     *
     * @param declareId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteInfoById(Integer declareId) {
        return this.billDeclareInfoDao.deleteInfoById(declareId) > 0;
    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public BillDeclareFile queryFileById(Integer id) {
        return this.billDeclareInfoDao.queryFileById(id);
    }

    @Override
    public BigDecimal selectLastAmount(BillDeclareProductInfo pro) {
        return billDeclareInfoDao.selectLastAmount(pro);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<BillDeclareFile> queryAllFileByLimit(int offset, int limit) {
        return this.billDeclareInfoDao.queryAllFileByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param billDeclareFile 实例对象
     * @return 实例对象
     */
    @Override
    public BillDeclareFile insertFile(BillDeclareFile billDeclareFile) {
        this.billDeclareInfoDao.insertFile(billDeclareFile);
        return billDeclareFile;
    }

    /**
     * 修改数据
     *
     * @param billDeclareFile 实例对象
     * @return 实例对象
     */
    @Override
    public BillDeclareFile updateFile(BillDeclareFile billDeclareFile) {
        this.billDeclareInfoDao.updateFile(billDeclareFile);
        return this.queryFileById(billDeclareFile.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteFileById(Integer id) {
        return this.billDeclareInfoDao.deleteFileById(id) > 0;
    }
    /**
     * 通过ID查询单条数据
     *
     * @param declareProductId 主键
     * @return 实例对象
     */
    @Override
    public BillDeclareProductInfo queryProductInfoById(Integer declareProductId) {
        return this.billDeclareInfoDao.queryProductInfoById(declareProductId);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<BillDeclareProductInfo> queryAllProductInfoByLimit(int offset, int limit) {
        return this.billDeclareInfoDao.queryAllProductInfoByLimit(offset, limit);
    }

    @Override
    public List<BillDeclareProductInfo> queryAllProductInfoByBillId(Integer billId) {
        return billDeclareInfoDao.queryAllProductInfoByBillId(billId);
    }

    /**
     * 新增数据
     *
     * @param billDeclareProductInfo 实例对象
     * @return 实例对象
     */
    @Override
    public BillDeclareProductInfo insertProductInfo(BillDeclareProductInfo billDeclareProductInfo) {
        this.billDeclareInfoDao.insertProductInfo(billDeclareProductInfo);
        return billDeclareProductInfo;
    }

    /**
     * 修改数据
     *
     * @param billDeclareProductInfo 实例对象
     * @return 实例对象
     */
    @Override
    public BillDeclareProductInfo updateProductInfo(BillDeclareProductInfo billDeclareProductInfo) {
        this.billDeclareInfoDao.updateProductInfo(billDeclareProductInfo);
        return this.queryProductInfoById(billDeclareProductInfo.getDeclareProductId());
    }

    /**
     * 通过主键删除数据
     *
     * @param declareProductId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteProductInfoById(Integer declareProductId) {
        return this.billDeclareInfoDao.deleteProductInfoById(declareProductId) > 0;
    }

    @Override
    public List<CarryDetailEntity> queryCarryBySearchBillDeclare(carryInfoSearch search) {
        return billDeclareInfoDao.queryCarryBySearchBillDeclare(search);
    }

    @Override
    public List<CarryDetailProductEntity> queryCarryProductByCarryId(Integer id) {
        return billDeclareInfoDao.queryCarryProductByCarryId(id);
    }

    @Override
    public List<CarryDetailProductPartsEntity> selectPartsByProduct(CarryDetailProductEntity productEntity) {
        return billDeclareInfoDao.selectPartsByProduct( productEntity) ;
    }

    @Override
    public logo selectLogo(Integer declare_id) {
        return billDeclareInfoDao.selectLogo(declare_id);
    }

    @Override
    public String selectpickupdate(String billNo) {
        return billDeclareInfoDao.selectpickupdate(billNo);
    }

    @Override
    public List<BillDeclareInfo> checkBillNo(String invoicenumber) {
        return billDeclareInfoDao.checkBillNo(invoicenumber);
    }

    @Override
    public List<BillDeclareInfo> selectStatSearch(statSearch search) {
        return billDeclareInfoDao.selectStatSearch(search);
    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public BillDeclareLog queryLogById(Integer id) {
        return this.billDeclareInfoDao.queryLogById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<BillDeclareLog> queryAllLogByLimit(int offset, int limit) {
        return this.billDeclareInfoDao.queryAllLogByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param billDeclareLog 实例对象
     * @return 实例对象
     */
    @Override
    public BillDeclareLog insertLog(BillDeclareLog billDeclareLog) {
        this.billDeclareInfoDao.insertLog(billDeclareLog);
        return billDeclareLog;
    }

    /**
     * 修改数据
     *
     * @param billDeclareLog 实例对象
     * @return 实例对象
     */
    @Override
    public BillDeclareLog updateLog(BillDeclareLog billDeclareLog) {
        this.billDeclareInfoDao.updateLog(billDeclareLog);
        return this.queryLogById(billDeclareLog.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteLogById(Integer id) {
        return this.billDeclareInfoDao.deleteLogById(id) > 0;
    }

}